package com.xinfu.newproject.model.matter_repair;

import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.matter_repair.RepairDetailBean;
import com.xinfu.newproject.bean.matter_repair.RepairDetailBody;
import com.xinfu.newproject.bean.matter_repair.RepairRecordBean;
import com.xinfu.newproject.bean.matter_repair.RepairRecordBody;
import com.xinfu.newproject.bean.matter_repair.bean.RepairCountBean;
import com.xinfu.newproject.bean.matter_repair.body.RepairCountBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RepairRecordModel implements IModel.IRepairRecordModel {

    /**
     * 报修记录列表
     *
     * @param repairRecordBody
     * @param callBack
     */
    @Override
    public void repairRecord(RepairRecordBody repairRecordBody, MyCallBack<RepairRecordBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", repairRecordBody.getUserId());

        JsNetworkService.getInstance().getIJsNetworkService()
                .queryRepairRecord(repairRecordBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RepairRecordBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull RepairRecordBean repairRecordBean) {

                        callBack.onSuccess(repairRecordBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 报修记录详情
     * @param repairDetailBody
     * @param callBack
     */
    @Override
    public void repairDetail(RepairDetailBody repairDetailBody, MyCallBack<RepairDetailBean> callBack) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("repairId", repairDetailBody.getRepairId());

        JsNetworkService.getInstance().getIJsNetworkService()
                .queryRepairDetail(repairDetailBody.getHeader(), repairDetailBody.getRepairId())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RepairDetailBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull RepairDetailBean repairDetailBean) {

                        callBack.onSuccess(repairDetailBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 报修记录数量
     *
     * @param repairCountBody
     * @param callBack
     */
    @Override
    public void repairCount(RepairCountBody repairCountBody, MyCallBack<RepairCountBean> callBack) {
        JsNetworkService.getInstance().getIJsNetworkService()
                .repairCount(repairCountBody.getHeader(), repairCountBody.getUserId())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RepairCountBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull RepairCountBean repairCountBean) {

                        callBack.onSuccess(repairCountBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
